﻿/*
创建人：longb
日期：2022-07-11
描述：材料申领单js
*/
var page = {
    pagetotal: 0,
    pageSize: 20,
    pageNumber: 1,
    ajaxUrl: getRootPath() + "/Ashx/ZsHandler/ZSNotProductMaterialApplyBillHandler.ashx",
    //初始化页面
    init: function () {
        page.bind();
        customizedEasyuiPagination('pp'); //定制化分页
    },
    //页面事件绑定
    bind: function () {
        var yearmonth=[];
        for(var i=new Date().getFullYear();i>=new Date().getFullYear()-5;i--){
            yearmonth.push({id:i,text:i});
        }
        $('#yearmonth').combobox({
            data: yearmonth,
            valueField: 'id',
            textField: 'text',
        });
        page.post({action:"GetWorkcenterByFactory"}, function (res) {
            $('#WorkcenterID').combobox({
                data: res,
                valueField: 'WORKCENTERID',
                textField: 'WORKCENTERNAME',
                onSelect: function (record) {
                    page.GetTeamByWorkcenter(record.WORKCENTERID);
                }
            });
        });
       
        //申领单信息
        $("#yearTable").datagrid({
            checkOnSelect: true,
            selectOnCheck: true,
            singleSelect: false,
            rownumbers: true,
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            columns: [[
                 { field: 'ZSNPMATERIALAPPLYBILLID', width: 120, checkbox: true },
                 { field: 'MATERIALCODE', title: '物料编码', width: 120 },
                 { field: 'MATERIALDESC', title: '物料描述', width: 120 },
		         { field: 'WORKCENTERNAME', title: '中心（部门）', width: 80 }, //--
                 { field: 'TEAMNAME', title: '班组（科室）', width: 90 }, 
                 { field: 'APPLYDATE', title: '年份', width: 90 }, 
                 { field: 'QUOTAQTY', title: '年度消耗定额', width: 90 }, 
                 { field: 'APPLYQTY', title: '年度消耗总数', width: 90 },  
                 { field: 'QTY', title: '超额数量', width: 90,formatter: function(value,row,index){return row.APPLYQTY-row.QUOTAQTY} } 
	           ]],
          onCheck:function(){page.GetSumNPMaterialByMonth();},
          onUncheck:function(){page.GetSumNPMaterialByMonth();},
          onCheckAll:function(){page.GetSumNPMaterialByMonth();}
        });
         $("#monthTable").datagrid({
         rownumbers: true,
          columns: [[
                 { field: 'APPLYDATE', title: '月份', width: 90 }, 
                 { field: 'QUOTAQTY', title: '月消耗定额', width: 90 }, 
                 { field: 'APPLYQTY', title: '月消耗总数', width: 90 },  
                 { field: 'QTY', title: '超额数量', width: 90,formatter: function(value,row,index){return row.APPLYQTY-row.QUOTAQTY} } 
	           ]],
         });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.GetSumNPMaterialByYear();
            }
        });
        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            page.pageNumber = pageNumber;
            page.GetSumNPMaterialByYear();
        });
        //清除按钮
        $('#btnReset').on("click", function () {
            page.reset(true, true);
            page.pageNumber = 1; page.pagetotal = 0
            $('#pp').pagination('refresh', {
                total: page.pagetotal,
                pageSize: page.pageSize,
                pageNumber: page.pageNumber
            });
        });
    },
    //查询
    searchData: function () {
        page.reset(false, true);
        page.pageNumber = 1;
        page.GetSumNPMaterialByYear();
    },
    GetTeamByWorkcenter:function(WorkcenterID){
     page.post({action:"GetTeamByWorkcenter",workcenterId:WorkcenterID}, function (res) {
            $('#TeamID').combobox({
                data: res,
                valueField: 'TEAMID',
                textField: 'TEAMNAME',
                onSelect: function (record) {
                    
                }
            });
        });
    },
    //查询材料申领单信息
    GetSumNPMaterialByYear: function () {
        $('#monthTable').datagrid('loadData',[]);
        var frdata = $('#searchForm').serializeArray();
        var data = {};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var param = {
            action: "GetSumNPMaterialByYear",
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            data: JSON.stringify([data])
        };
        page.post(param, function (res) {
            if (res.Result) {
                //console.info(res);
                page.pagetotal = res.RowCount;
                $('#yearTable').datagrid('loadData', res.DBTable);
            }
        });
    },
    GetSumNPMaterialByMonth:function(){
        var rows=$('#yearTable').datagrid("getChecked");
        if(rows.length==0){ $('#monthTable').datagrid('loadData', []);return;}
        var data = [];
        $.each(rows, function (index,item) {
           data.push(item.MATERIALCODE);
        });
        var param = {
            action: "GetSumNPMaterialByMonth",
            WorkcenterID:$('#WorkcenterID').combobox('getValue'),
            yearmonth:$('#yearmonth').combobox('getValue'),
            ids: data.join(',')
        };
        page.post(param, function (res) {
            $('#monthTable').datagrid('loadData', res.DBTable);
        });
    
    },
    //查询重置
    searchReset: function () {
        $('#searchForm').form('clear');
        //page.bind();
    },

    //清除方法，重要！！！
    reset: function (iscondition, isresult) {
        clearMessage();
    },
    //post数据
    post: function (param, callback) {
        $.ajax({
            type: "Post",
            url: page.ajaxUrl,
            data: param,
            cashe: false,
            async: true,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    }
}
$(function () {
    page.init();
});